<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!-- 用于切换显示状态的按钮组
         1:资产2:负债3:权益4:成本5:损益 -->

      <!-- <el-form-item label="科目编码" prop="subjectCode">
                    <el-input
                        v-model="queryParams.subjectCode"
                        placeholder="请输入科目编码"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="科目名称" prop="subjectName">
                    <el-input
                        v-model="queryParams.subjectName"
                        placeholder="请输入科目名称"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="助记码" prop="subjectMnemonic">
                    <el-input
                        v-model="queryParams.subjectMnemonic"
                        placeholder="请输入助记码"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="数量核算(计量单位)" prop="subjectQuantityAccounting">
                    <el-input
                        v-model="queryParams.subjectQuantityAccounting"
                        placeholder="请输入数量核算(计量单位)"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="余额(在凭证回显的)" prop="balance">
                    <el-input
                        v-model="queryParams.balance"
                        placeholder="请输入余额(在凭证回显的)"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="科目种类(
1:资产
2:负债
3:权益
4:成本
5:损益)" prop="subjectTypeId">
                    <el-input
                        v-model="queryParams.subjectTypeId"
                        placeholder="请输入科目种类(
1:资产
2:负债
3:权益
4:成本
5:损益)"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="父级ID" prop="parentId">
                    <el-input
                        v-model="queryParams.parentId"
                        placeholder="请输入父级ID"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="账套外键ID" prop="accountId">
                    <el-input
                        v-model="queryParams.accountId"
                        placeholder="请输入账套外键ID"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="期初余额" prop="subjectOpeningBalance">
                    <el-input
                        v-model="queryParams.subjectOpeningBalance"
                        placeholder="请输入期初余额"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="期初数量" prop="subjectOpeningNum">
                    <el-input
                        v-model="queryParams.subjectOpeningNum"
                        placeholder="请输入期初数量"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="借方累计(累计金额)" prop="subjectCumulativeDebitAmount">
                    <el-input
                        v-model="queryParams.subjectCumulativeDebitAmount"
                        placeholder="请输入借方累计(累计金额)"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="借方数量" prop="subjectCumulativeDebitNum">
                    <el-input
                        v-model="queryParams.subjectCumulativeDebitNum"
                        placeholder="请输入借方数量"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="贷方累计(累计金额)" prop="subjectCumulativeCreditAmount">
                    <el-input
                        v-model="queryParams.subjectCumulativeCreditAmount"
                        placeholder="请输入贷方累计(累计金额)"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="贷方数量" prop="subjectCumulativeCreditNum">
                    <el-input
                        v-model="queryParams.subjectCumulativeCreditNum"
                        placeholder="请输入贷方数量"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="科目还是期初(0:在科目添加,在期初也会回显,1:只会在期初中显示)" prop="subjectOrOpening">
                    <el-input
                        v-model="queryParams.subjectOrOpening"
                        placeholder="请输入科目还是期初(0:在科目添加,在期初也会回显,1:只会在期初中显示)"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="备注(通用字段)" prop="remark">
                    <el-input
                        v-model="queryParams.remark"
                        placeholder="请输入备注(通用字段)"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="修改时间" prop="updateTime">
                    <el-date-picker clearable
                                    v-model="queryParams.updateTime"
                                    type="date"
                                    value-format="yyyy-MM-dd"
                                    placeholder="请选择修改时间">
                    </el-date-picker>
                  </el-form-item>
                  <el-form-item label="修改人" prop="updateBy">
                    <el-input
                        v-model="queryParams.updateBy"
                        placeholder="请输入修改人"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="创建时间" prop="createTime">
                    <el-date-picker clearable
                                    v-model="queryParams.createTime"
                                    type="date"
                                    value-format="yyyy-MM-dd"
                                    placeholder="请选择创建时间">
                    </el-date-picker>
                  </el-form-item>
                  <el-form-item label="创建人" prop="createBy">
                    <el-input
                        v-model="queryParams.createBy"
                        placeholder="请输入创建人"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item> -->

      <el-form-item prop="subjectName">
        <el-checkbox v-model="checked1" label="显示停用科目" size="large" @change="(val) => getByStatus()" />
      </el-form-item>

      <el-form-item label="租户外键id" prop="tenantId">
        <el-input v-model="queryParams.tenantId" placeholder="请输入租户外键id" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['subject:subject:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['subject:subject:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['subject:subject:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['subject:subject:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="subjectList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="科目主键ID" align="center" prop="id" /> -->
      <el-table-column label="科目编码" align="center" prop="subjectCode" />
      <el-table-column label="科目名称" align="center" prop="subjectName" />
      <el-table-column label="助记码" align="center" prop="subjectMnemonic" />
      <el-table-column label="余额方向" align="center" prop="subjectBalanceDirectionStatus">
        <template #default="scope">
          {{ scope.row.subjectBalanceDirectionStatus == 0 ? "借" : "贷" }}
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="现金以及现金等价物0:不勾选1:勾选"
        align="center"
        prop="subjectCashAndCashEquivalents"
      /> -->
      <el-table-column label="数量核算(计量单位)" align="center" prop="subjectQuantityAccounting" />
      <el-table-column label="外币核算" align="center" prop="subjectForeignCurrencyAccounting" />
      <el-table-column label="辅助核算(例:客户,供应商,职员)" align="center" prop="subjectAuxiliaryAccounting" />
      <!-- <el-table-column
        label="非必填辅助核算(这里边有的是在添加期初的时候可以空着的对象)"
        align="center"
        prop="subjectAuxiliaryAccountingOptional"
      /> -->
      <!-- <el-table-column
        label="余额(在凭证回显的)"
        align="center"
        prop="balance"
      /> -->
      <!-- <el-table-column
        label="科目种类(1:资产2:负债3:权益4:成本5:损益)"
        align="center"
        prop="subjectTypeId"
      /> -->
      <el-table-column label="状态" align="center" prop="subjectStatus">
        <template #default="scope">
          <el-switch v-model="scope.row.subjectStatus" active-text="开启" inactive-text="关闭" :active-value="1"
            :inactive-value="0" @change="(event) => changeStatus(scope.row)" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="父级ID" align="center" prop="parentId" /> -->
      <!-- <el-table-column label="账套外键ID" align="center" prop="accountId" /> -->
      <el-table-column label="期初余额" align="center" prop="subjectOpeningBalance" />
      <el-table-column label="期初数量" align="center" prop="subjectOpeningNum" />
      <el-table-column label="借方累计(累计金额)" align="center" prop="subjectCumulativeDebitAmount" />
      <el-table-column label="借方数量" align="center" prop="subjectCumulativeDebitNum" />
      <el-table-column label="贷方累计(累计金额)" align="center" prop="subjectCumulativeCreditAmount" />
      <el-table-column label="贷方数量" align="center" prop="subjectCumulativeCreditNum" />
      <!-- <el-table-column
        label="科目还是期初(0:在科目添加,在期初也会回显,1:只会在期初中显示)"
        align="center"
        prop="subjectOrOpening"
      /> -->
      <!-- <el-table-column label="备注(通用字段)" align="center" prop="remark" /> -->
      <!-- <el-table-column
        label="修改时间"
        align="center"
        prop="updateTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="修改人" align="center" prop="updateBy" /> -->
      <!-- <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="租户外键id" align="center" prop="tenantId" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['subject:subject:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['subject:subject:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改科目/期初对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="科目编码" prop="subjectCode">
          <el-input v-model="form.subjectCode" placeholder="请输入科目编码" />
        </el-form-item>
        <el-form-item label="科目名称" prop="subjectName">
          <el-input v-model="form.subjectName" placeholder="请输入科目名称" />
        </el-form-item>
        <el-form-item label="助记码" prop="subjectMnemonic">
          <el-input v-model="form.subjectMnemonic" placeholder="请输入助记码" />
        </el-form-item>
        <el-form-item label="数量核算(计量单位)" prop="subjectQuantityAccounting">
          <el-input v-model="form.subjectQuantityAccounting" placeholder="请输入数量核算(计量单位)" />
        </el-form-item>
        <el-form-item label="余额(在凭证回显的)" prop="balance">
          <el-input v-model="form.balance" placeholder="请输入余额(在凭证回显的)" />
        </el-form-item>
        <el-form-item label="科目种类(1:资产2:负债3:权益4:成本5:损益)" prop="subjectTypeId">
          <el-input v-model="form.subjectTypeId" placeholder="请输入科目种类(1:资产2:负债3:权益4:成本5:损益)" />
        </el-form-item>
        <el-form-item label="父级ID" prop="parentId">
          <el-input v-model="form.parentId" placeholder="请输入父级ID" />
        </el-form-item>
        <el-form-item label="账套外键ID" prop="accountId">
          <el-input v-model="form.accountId" placeholder="请输入账套外键ID" />
        </el-form-item>
        <el-form-item label="期初余额" prop="subjectOpeningBalance">
          <el-input v-model="form.subjectOpeningBalance" placeholder="请输入期初余额" />
        </el-form-item>
        <el-form-item label="期初数量" prop="subjectOpeningNum">
          <el-input v-model="form.subjectOpeningNum" placeholder="请输入期初数量" />
        </el-form-item>
        <el-form-item label="借方累计(累计金额)" prop="subjectCumulativeDebitAmount">
          <el-input v-model="form.subjectCumulativeDebitAmount" placeholder="请输入借方累计(累计金额)" />
        </el-form-item>
        <el-form-item label="借方数量" prop="subjectCumulativeDebitNum">
          <el-input v-model="form.subjectCumulativeDebitNum" placeholder="请输入借方数量" />
        </el-form-item>
        <el-form-item label="贷方累计(累计金额)" prop="subjectCumulativeCreditAmount">
          <el-input v-model="form.subjectCumulativeCreditAmount" placeholder="请输入贷方累计(累计金额)" />
        </el-form-item>
        <el-form-item label="贷方数量" prop="subjectCumulativeCreditNum">
          <el-input v-model="form.subjectCumulativeCreditNum" placeholder="请输入贷方数量" />
        </el-form-item>
        <el-form-item label="科目还是期初(0:在科目添加,在期初也会回显,1:只会在期初中显示)" prop="subjectOrOpening">
          <el-input v-model="form.subjectOrOpening" placeholder="请输入科目还是期初(0:在科目添加,在期初也会回显,1:只会在期初中显示)" />
        </el-form-item>
        <el-form-item label="备注(通用字段)" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注(通用字段)" />
        </el-form-item>
        <el-form-item label="租户外键id" prop="tenantId">
          <el-input v-model="form.tenantId" placeholder="请输入租户外键id" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listSubject,
  getSubject,
  delSubject,
  addSubject,
  updateSubject,
} from "@/api/subject/subject";
import { parseTime } from "@/utils/vctgo";

export default {
  name: "Subject",
  data() {
    return {
      checked1: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 科目/期初表格数据
      subjectList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        subjectCode: null,
        subjectName: null,
        subjectMnemonic: null,
        subjectBalanceDirectionStatus: null,
        subjectCashAndCashEquivalents: null,
        subjectQuantityAccounting: null,
        subjectForeignCurrencyAccounting: null,
        subjectAuxiliaryAccounting: null,
        subjectAuxiliaryAccountingOptional: null,
        balance: null,
        subjectTypeId: null,
        subjectStatus: null,
        parentId: null,
        accountId: null,
        subjectOpeningBalance: null,
        subjectOpeningNum: null,
        subjectCumulativeDebitAmount: null,
        subjectCumulativeDebitNum: null,
        subjectCumulativeCreditAmount: null,
        subjectCumulativeCreditNum: null,
        subjectOrOpening: null,
        remark: null,
        updateTime: null,
        updateBy: null,
        createTime: null,
        createBy: null,
        tenantId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },

  methods: {
    /** 查询科目/期初列表 */
    getList() {
      this.loading = true;
      this.queryParams.subjectOrOpening = 0;
      listSubject(this.queryParams).then((response) => {
        this.subjectList = response.data;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 根据状态选择
    getByStatus() {
      if (this.checked1 == true) {
        this.queryParams.subjectStatus = 0;
        console.log(this.queryParams.subjectStatus);
      } else {
        this.queryParams.subjectStatus = 1;
        console.log(this.queryParams.subjectStatus);
      }
      listSubject(this.queryParams).then((response) => {
        this.subjectList = response.data;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 开关修改状态按钮
    changeStatus(row) {
      updateSubject(row).then((response) => {
        this.$modal.msgSuccess("修改成功");
        this.open = false;
        this.getList();
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        subjectCode: null,
        subjectName: null,
        subjectMnemonic: null,
        subjectBalanceDirectionStatus: 0,
        subjectCashAndCashEquivalents: 0,
        subjectQuantityAccounting: null,
        subjectForeignCurrencyAccounting: [],
        subjectAuxiliaryAccounting: [],
        subjectAuxiliaryAccountingOptional: [],
        balance: null,
        subjectTypeId: null,
        subjectStatus: 0,
        parentId: null,
        accountId: null,
        subjectOpeningBalance: null,
        subjectOpeningNum: null,
        subjectCumulativeDebitAmount: null,
        subjectCumulativeDebitNum: null,
        subjectCumulativeCreditAmount: null,
        subjectCumulativeCreditNum: null,
        subjectOrOpening: null,
        remark: null,
        updateTime: null,
        updateBy: null,
        createTime: null,
        createBy: null,
        tenantId: null,
      };
      this.resetForm("form");
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加科目/期初";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getSubject(id).then((response) => {
        this.form = response.data;
        this.form.subjectForeignCurrencyAccounting =
          this.form.subjectForeignCurrencyAccounting.split(",");
        this.form.subjectAuxiliaryAccounting =
          this.form.subjectAuxiliaryAccounting.split(",");
        this.form.subjectAuxiliaryAccountingOptional =
          this.form.subjectAuxiliaryAccountingOptional.split(",");
        this.open = true;
        this.title = "修改科目/期初";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.subjectForeignCurrencyAccounting =
            this.form.subjectForeignCurrencyAccounting.join(",");
          this.form.subjectAuxiliaryAccounting =
            this.form.subjectAuxiliaryAccounting.join(",");
          this.form.subjectAuxiliaryAccountingOptional =
            this.form.subjectAuxiliaryAccountingOptional.join(",");
          if (this.form.id != null) {
            updateSubject(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSubject(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("是否确认删除当前科目/期初数据项？")
        .then(function () {
          return delSubject(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "subject/subject/export",
        {
          ...this.queryParams,
        },
        `科目/期初_${parseTime(new Date().getTime(), "{y}-{m}-{d}")}.xlsx`
      );
    },
  },
};
</script>
